package com.hong.servicehosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hong.commonutil.result.Result;
import com.hong.model.model.hosp.HospitalSet;
import com.hong.model.vo.hosp.HospitalSetQueryVo;
import com.hong.servicehosp.service.HospitalSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = {"医院设置管理"})
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院的设置信息")
    @GetMapping("findAll")//http://localhost:8201/admin/hosp/hospitalSet/findAll
    public Result findAllHospitalSet() {
        List<HospitalSet> allHospitalSet = hospitalSetService.findAllHospitalSet();
        return Result.ok(allHospitalSet);
    }

    @ApiOperation(value = "根据id逻辑删除数据")
    @DeleteMapping("{id}")
    public Result deleteHospitalSetById(@PathVariable Long id) {
        boolean flag = hospitalSetService.deleteHospitalSetById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "带分页的条件查询")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospitalSet(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象进行分页
        Page<HospitalSet> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        //获取传过来的查询条件
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hoscode);
        }
        wrapper.eq("is_deleted", 0);
        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
//        List<HospitalSet> hospitalSets =hospitalSetService.findPageHospitalSet(current,limit,hospitalSetQueryVo);
//        return Result.ok(hospitalSets);
    }
    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        int saveCount = hospitalSetService.saveHospitalSet(hospitalSet);
        if (saveCount == 1) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("hospitalSet/{id}")
    public Result hospitalSet(@PathVariable long id) {
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetById(id);
        if (hospitalSet != null) {
            return Result.ok(hospitalSet);
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "修改医院设置")
    @PutMapping("hospitalSet")
    public Result updateHospitalSetById(@RequestBody HospitalSet hospitalSet) {
        int count = hospitalSetService.updateHospitalSetById(hospitalSet);
        if (count == 1) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchDeleteHospitalSet")
    public Result batchDeleteHospitalSet(@RequestBody List<Long> ids) {
        int count = hospitalSetService.batchDeleteHospitalSet(ids);
        if (count == ids.size()) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("hospitalSetLockOrUnlock/{id}/{status}")
    public Result hospitalSetLockOrUnlock(@PathVariable Long id, @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        int count = hospitalSetService.updateHospitalSetById(hospitalSet);
        if (count == 1) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
//    @ApiOperation(value = "发送签名秘钥")
//    @PutMapping("sendKey/{id}")
//    public Result sendKey(@PathVariable Long id){
//        HospitalSet hospitalSet = hospitalSetService.getHospitalSetById(id);
//        String signKey = hospitalSet.getSignKey();
//        String hoscode = hospitalSet.getHoscode();
//    }

}
